package com.SinaMicroblogging;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import com.SinaMicroblogging.ApplicationConfig.ApplicationConfig;
import com.SinaMicrobloggingApi.Configuration.SinaAPIUrlConfig;
import com.SinaMicrobloggingApi.Model.UserInfoModel;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.res.XmlResourceParser;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class PageBase extends Activity {

	private UserInfoModel currentUser;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

	}

	public void showMsgBox(String str) {
		new AlertDialog.Builder(this).setTitle("Message").setMessage(str)
				.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dlg, int something) {
					}
				}).show();
	}

	public void showToast(String str) {
		Toast toast = Toast.makeText(getApplicationContext(), str,
				Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}

	public void showActionMsgBox(String... str) {
		LayoutInflater factory = LayoutInflater.from(this);
		View actionView = factory.inflate(R.layout.actiondialog, null);
		ListView listView = (ListView) actionView.findViewById(R.id.actions);
		ListAdapter apdater = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, str);
		listView.setAdapter(apdater);
		final AlertDialog dialog = new AlertDialog.Builder(this).setTitle(
				"Choose").setView(actionView).show();
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				if (arg2 == 3) {
					dialog.cancel();
				}
			}
		});

	}

	public UserInfoModel getCurrentUser() {
		if (currentUser == null) {
			ApplicationConfig applicationConfig = (ApplicationConfig) this
					.getApplication();
			currentUser = applicationConfig.getUser();
		}
		return currentUser;
	}

	protected String[] loadLoggingUserAccess(String userName) {
		Properties p = new Properties();
		try {
			FileInputStream fs = this.openFileInput("logginguser.cfg");
			try {
				p.load(fs);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		String[] access = new String[2];
		access[0] = (String) p.get(String.format("%s_AccessToken", userName));
		access[1] = (String) p.get(String.format("%s_AccessTokenSecret",
				userName));
		return access;
	}

	protected void saveLoggingUserAccess(String userName, UserInfoModel user) {
		if (user != null) {
			Properties p = new Properties();
			p.put(String.format("%s_AccessToken", userName), user
					.getAccessToken());
			p.put(String.format("%s_AccessTokenSecret", userName), user
					.getAccessTokenSecret());
			try {
				FileOutputStream fs = this.openFileOutput("logginguser.cfg",
						Context.MODE_WORLD_WRITEABLE);
				try {
					p.store(fs, "sinaweibo");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
}